8.6 TRUExpress - A Simple Express-like Library

  1. Learning outcomes
    • Learn and practice JavaScript more through the designing and implementation if a simple Express-like library.

  2. Observations on Express
    • Let's check again the typical coding style to use Express.
      const express = require("express");
      const app = express();  // what is express?
                              // what is app?
      app.listen(portnumber);
      app.get(path, function(req,res) {  
      });
      app.post(..., ...);
      app.put(..., ...);
      app.delete(..., ...);
      ...
      
    • We can see that express is a function and it returns an object. The return object has multiple methods.
    • app.listen() is used to listen to a HTTP port number. app.method() is used to register a callback function for a path. It can be used multiple times for differnt paths.
    • An http server is created when express() is envoked, so that when an http packet arrives, the path in URL is checked, a corresponding callback function is searched and invoked with the request and the response objects.

  3. Requirements for a simple Express-like library, called TRUExpress
    • const truexpress = require("tru-express.js");
      const app = truexpress();
      
    • app.listen()
    • app.get()

  4. How to implement TRUExpress
    • tru-express.js
      • const truexpress = function() {
            // initialization
            ...
            
            // return an object of methods
            return {
                listen: function(port) {
                    ...
                },
                get: function(path, callback) {  // It can be invoked multiple times for different paths.
                    ...
                },
            }
        }
        module.exports = truexpress;
        

    • Initialization in tru-express.js
      • Note that TRUExpress supports an app web server.
      • An http server must be implemented. For example,
        const http = require("node:http");
        
        const server = http.createServer((request, response) => {  // request: from the client; response: to the client
            ...
        });
        
        server.listen(PORT_NO);
        
      • What must be included in the initialization? Definitely not listening to a port number yet because ...

    • listen() in tru-express.js
      • What must be included? Listening to the given port number.

    • get() in tru-express.js
      • Note that the role of this method is to register a callback function for a given path, which will be invoked when an HTTP GET request that includes the given path comes.
      • How to register? (Note that listeners for multiple GET routes can be registered.)
        let getCallbacks = [];
        ...
        // the get() method in the return object
        get: function (path, callbackk) {
            getCallbacks.push({path:path, callback:callback});
        }
        
      • How to use? When an HTTP request comes,
        const url = require("node:url");
        ...
        // In server.createServer((request, response) => { ... });
        if (request.method.toLowerCase() == "get") {
            const path = decodeURI(url.parse(request.url).pathname);
            ...  // search getCallbasks to find the above path and get callback
            callback(request, response);
        }
        

    • Now you can combine them all.

    • Trial 1: Let's try ...